<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 问答控制器
 */
class Wenda extends MY_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $this->display('wenda/index.html');
    }
    //问答详情页
    function answer() {
        $wenda_id = isset($_GET['wenda_id']) ? decrypt_wenda_id(trim($_GET['wenda_id'])) : "4";
        $user_id = isset($_GET['user_id']) ? decrypt_user_id(trim($_GET['user_id'])) : 0;
        $item = $this->db->query("select w.id,w.title,w.tags_name,w.questioner_id,w.content,w.summary,w.create_time,i.head_img,i.nickname from tan_bbs_wenda w left join tan_users_info i on i.user_id = w.questioner_id where w.id = $wenda_id and w.is_show = 1 and w.status = 1 order by w.id desc limit 1")->row_array();
        if(!$item){
            $this->display('deleted/index.html');exit;
        }
        $tags = array();
        if($item['tags_name']){
            $tags_arr = explode(",", $item['tags_name']);
            foreach ($tags_arr as $v){
                $v = explode("_", $v);
                $tags[$v[1]] = array("tid"=>$v[1],"tname"=>$v[0]);
            }
        }
        $item['tags'] = $tags;
        $item['is_care'] = $user_id ? ($this->db->select("*")->from("bbs_wenda_care")->where(array("user_id"=>$user_id,"wenda_id"=>$wenda_id,"is_del"=>1))->order_by("id")->get()->num_rows() ? 1 : 0) : 0;
        $this->db->select("*")->from("bbs_wenda_answer")->where(array("user_id"=>$user_id,"wenda_id"=>$wenda_id))->order_by("id")->get()->num_rows();
        $item['is_self'] = $user_id == $item['questioner_id'];
        $item['wenda_id'] = encrypt_wenda_id($wenda_id);
        $item['questioner_id'] = encrypt_user_id($wenda_id);
        $item['is_answer'] = $user_id ? ($this->db->select("*")->from("bbs_wenda_answer")->where(array("user_id"=>$user_id,"wenda_id"=>$wenda_id))->order_by("id")->get()->num_rows() ? 1 : 0) : 0;
        $item['content'] = str_replace("div", "p", $item['content']);
        $item['title_s'] = mb_strcut($item['title'], 0, 30, "utf8")."...";
        $item['summary_s'] = strip_tags($item['summary']);
        $item['questioner_uid'] = encrypt_user_id($item['questioner_id']);
        $item['head_img'] = strstr($item['head_img'],"http") ? $item['head_img'] : $this->img_host.$item['head_img'];
        $item['create_time'] = $this->set_time_three($item['create_time']);

        $this->assign("title", $item['title']);

        $wenda = $this->db->select("id,questioner_id,answer_id,title,answernum")->from("bbs_wenda")->where(array("id"=>$wenda_id))->order_by("id")->get()->row_array();
        //回答区
        $list = $this->db->query("select a.id,a.user_id,a.content,a.likes,a.commentnum,a.is_answer,a.create_time,u.nickname,u.head_img from tan_bbs_wenda_answer a left join tan_users_info u on u.user_id = a.user_id where a.wenda_id = $wenda_id and a.is_show = 1 order by a.is_answer desc,a.likes desc,a.id desc limit 6")->result_array();
        foreach($list as $key=>$val){
            $list[$key]['is_like'] = $user_id ?($this->db->select("*")->from("bbs_wenda_answer_like")->where(array("user_id"=>$user_id,"answer_id"=>$val['id'],"is_del"=>1))->order_by("id")->get()->num_rows() ? 1 : 0) : 0;
            $list[$key]['id'] = encrypt_answer_id($val['id']);
            $list[$key]['user_id'] = encrypt_user_id($val['user_id']);
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            if($val['is_answer']){
                $list[$key]['status'] = 1;//最佳答案
            }else{
                if(!$wenda['answer_id']){
                    if($user_id == $wenda['questioner_id']){
                        $list[$key]['status'] = 2;//采纳为答案
                    }
                }
            }
            //匹配回答内容里所有图片
            preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/i",$val['content'],$matches);//带引号
            $new_arr = array_unique($matches[0]);//去除数组中重复的值

            if($new_arr){//显示图片
                foreach ($new_arr as $k=>$img){
                    if(($k + 1) <= 3){
                        $html [] = "<li>$img</li>";
                    }
                }
                $list[$key]['picture'] = $html;
            }

        }
        $this->assign("list", $list);
        $this->assign("item", $item);
        $this->display('wenda/answer.html');
    }
    //回答详情页
    function answer_detail() {
        $wenda_id = isset($_GET['wenda_id']) ? decrypt_wenda_id(trim($_GET['wenda_id'])) : 0;
        $answer_id = isset($_GET['answer_id']) ? decrypt_answer_id(trim($_GET['answer_id'])) : 0;
        $user_id = isset($_GET['user_id']) ? decrypt_user_id(trim($_GET['user_id'])) : 0;
        //判断是否存在该答案
        $item = $this->db->query("select a.*,i.head_img,i.nickname,w.title from tan_bbs_wenda_answer a left join tan_users_info i on i.user_id = a.user_id left join tan_bbs_wenda w on w.id = a.wenda_id where a.id = $answer_id and a.wenda_id = $wenda_id and a.is_show = 1 order by a.id desc limit 1")->row_array();
        if(!$item){
            $this->display('deleted/index.html');exit;
        }
        $this->assign("title", $item['title']);
        $repeat_str = array("<br>",'<p><span style="font-size: 13.3333px;"></span></p>',"<p><br></p>","<p>&nbsp;</p>",'<p><span style="color: rgb(51, 51, 51);">&nbsp;</span></p>','<p><span style="color: rgb(62, 62, 62);">&nbsp;</span></p>','<p><strong><span style="font-size: 10.5pt; color: black;">&nbsp;</span></strong></p>','<p><span style="font-size: 10.5pt; color: black;">&nbsp;</span></p>');
        $item['content'] = str_replace($repeat_str, "", $item['content']);
        $item['is_self'] = $user_id == $item['user_id'] ? 1 : 0;////0：是否是自己的回答
        $item['is_care'] = $user_id ? ($this->db->select("*")->from("bbs_fans")->where(array("followuid"=>$user_id,"user_id"=>$item['user_id'],"is_del"=>1))->order_by("id")->get()->num_rows() ? 1 : 0) : 0;//是否关注
        $item['is_like'] = $user_id ?($this->db->select("*")->from("bbs_wenda_answer_like")->where(array("user_id"=>$user_id,"answer_id"=>$answer_id,"is_del"=>1))->order_by("id")->get()->num_rows() ? 1 : 0) : 0;//是否点赞
        $item['answer_id'] = encrypt_answer_id($answer_id);
        $item['answer_uid'] = encrypt_user_id($item['user_id']);
        $item['head_img'] = strstr($item['head_img'],"http") ? $item['head_img'] : $this->img_host.$item['head_img'];
        $item['create_time'] = $this->set_time_three($item['create_time']);

        $list = $this->db->select("r.*,u1.head_img,u1.nickname,u2.nickname reply_nickname")->from("bbs_wenda_reply r")->join("users_info u1","u1.user_id=r.user_id","left")->join("users_info u2","u2.user_id=r.reply_uid","left")->where(array("r.answer_id"=>$answer_id,"r.is_show"=>1))->order_by("id asc")->limit(3)->get()->result_array();
        foreach($list as $key=>$val){
            $list[$key]['id'] = encrypt_answer_reply_id($val['id']);
            $list[$key]['create_time'] = $this->set_time_two($val['create_time']);
            $list[$key]['is_self'] = $user_id == $val['user_id'] ? 1 : 0;
            $list[$key]['lengther'] = mb_strlen($val['content'], "utf8");
            $list[$key]['head_img'] = strstr($val['head_img'],"http") ? $val['head_img'] : $this->img_host.$val['head_img'];
            if ($val['reply_uid']){
                $list[$key]['is_reply'] = "1";
                $list[$key]['reply_uid'] = encrypt_user_id($val['reply_uid']);
            }else{
                $list[$key]['is_reply'] = "0";
            }
        }
        $this->assign("item", $item);
        $this->assign("list", $list);
        $this->display('wenda/answer-detail.html');
    }
}
